package com.tony.blog.server.controller.admin;

import com.google.common.collect.Maps;
import com.tony.blog.api.response.BaseResponse;
import com.tony.blog.api.response.StatusCode;
import com.tony.blog.api.utils.PageUtil;
import com.tony.blog.server.annotation.LogAnnotation;
import com.tony.blog.server.controller.AbstractController;
import com.tony.blog.server.service.SysLogService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @Title: SysLogController
 * @Auther: 皮蛋布丁
 * @Date: 2021/08/02/21:56
 * @Description:
 */
@Controller
@RequestMapping("/sys/log")
public class SysLogController extends AbstractController {

    @Autowired
    private SysLogService sysLogService;

    /**
    * @Description: list 获取日志列表
    * @Param: [params]
    * @return: com.tony.pmp.common.response.BaseResponse
    * @Author: 皮蛋布丁
    * @Date: 2021/8/2 22:00
    */
    @ResponseBody
    @RequestMapping("/list")
    public BaseResponse list(@RequestParam Map<String,Object> params) {
        BaseResponse response = new BaseResponse(StatusCode.Success);
        Map<String,Object> resMap = Maps.newHashMap();
        try {
            log.info(">>>>>>日志信息列表的查询>>>>>>");

            PageUtil page = sysLogService.queryPage(params);
            resMap.put("page",page);
        } catch (Exception e) {
            response = new BaseResponse(StatusCode.Fail.getCode(),e.getMessage());
        }
        response.setData(resMap);
        return response;
    }

    /**
    * @Description: truncate 清除日志信息
    * @Param: []
    * @return: com.tony.pmp.common.response.BaseResponse
    * @Author: 皮蛋布丁
    * @Date: 2021/8/2 22:23
    */
    @LogAnnotation("清除日志信息")
    @ResponseBody
    @RequestMapping("/truncate")
    @RequiresPermissions("sys:log:truncate")
    public BaseResponse truncate() {
        BaseResponse response = new BaseResponse(StatusCode.Success);
        try {
            log.info("清除日志的状态为：{}",sysLogService.truncate());
        } catch (Exception e) {
            response = new BaseResponse(StatusCode.Fail.getCode(),e.getMessage());
        }
        return response;
    }

}
